<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
</body>
<script>
  // pid表示当前对象的父节点对象的id值
    const list = [
      { id: 1, pid: 0 },
      { id: 2, pid: 1 },
      { id: 3, pid: 2 },
      { id: 4, pid: 2 },
      { id: 5, pid: 4 },
      { id: 6, pid: 0 }
    ]
    // 处理树形数据结构主要是利用递归思想
    function  treeData(arr,pid){
        return res =arr.filter((item)=>{
          if(item.pid===pid){
            // 给当前找到的符合的对象添加一个chnildren属性来找到子对象对象的数据
            item.children=this.treeData(arr,item.id)
            return true
          }
        })
    }
    console.log(treeData(list,0));
</script>
</html>